package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.DiscretePathEffect
import android.graphics.Paint
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* PathEffectView02
* */
class PathEffectView02 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private var mPathEffect: DiscretePathEffect? = null
    
    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_9467DD)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_9467DD)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)

            if (mPathEffect == null) {
                //val circleLength = Math.PI * (cx * 2 / 3) * 2
                //val segmentLength = (circleLength / 32).toFloat()
                //mPathEffect = DiscretePathEffect(segmentLength, circleLength.toFloat() / 36)

                //mPathEffect = DiscretePathEffect(cx * 2 / 3 / 16, cx / 8)

                //mPathEffect = DiscretePathEffect(cx / 6, cx / 6)

                val circleLength = Math.PI * (cx * 3 / 4)
                val segmentLength = (circleLength / 32).toFloat()
                mPathEffect = DiscretePathEffect(segmentLength, circleLength.toFloat() / 62)
            }
            mPaint0.pathEffect = mPathEffect

            /*val baseX = cx * 2 / 3
            val baseY = cy * 2 / 3
            drawArc(
                -baseX,
                -baseY,
                baseX,
                baseY,
                0F,
                360F,
                false,
                mPaint0
            )*/

            /*drawRect(
                -baseX,
                -baseY,
                baseX,
                baseY,
                mPaint0
            )*/

            /*drawLine(
                -cx,
                0F,
                cx,
                0F,
                mPaint0
            )*/

            save()
            translate(0F, cx * 3 / 8)

            val baseX0 = cx * 3 / 4
            val baseY0 = cy * 3 / 4
            drawArc(
                -baseX0,
                -baseY0,
                baseX0,
                baseY0,
                180F,
                180F,
                false,
                mPaint1
            )

            drawArc(
                -baseX0,
                -baseY0,
                baseX0,
                baseY0,
                180F,
                180F,
                false,
                mPaint0
            )
            restore()
        }
    }
}